package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.PageInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.News;
import com.ruoyi.system.service.INewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * newsController
 * 
 * @author ruoyi
 * @date 2025-08-05
 */
@Controller
@RequestMapping("/system/news")
public class NewsController extends BaseController
{
    @Autowired
    private INewsService newsService;

    /**
     * 查询news列表
     */
    @PreAuthorize("@ss.hasPermi('system:news:list')")
    @GetMapping("/list")
    public TableDataInfo list(News news)
    {
        startPage();
        List<News> list = newsService.selectNewsList(news);
        return getDataTable(list);
    }

    /**
     * 导出news列表
     */
    @PreAuthorize("@ss.hasPermi('system:news:export')")
    @Log(title = "news", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, News news)
    {
        List<News> list = newsService.selectNewsList(news);
        ExcelUtil<News> util = new ExcelUtil<News>(News.class);
        util.exportExcel(response, list, "news数据");
    }

    /**
     * 获取news详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:news:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(newsService.selectNewsById(id));
    }

    /**
     * 新增news
     */
    @PreAuthorize("@ss.hasPermi('system:news:add')")
    @Log(title = "news", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody News news)
    {
        return toAjax(newsService.insertNews(news));
    }

    /**
     * 修改news
     */
    @PreAuthorize("@ss.hasPermi('system:news:edit')")
    @Log(title = "news", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody News news)
    {
        return toAjax(newsService.updateNews(news));
    }

    /**
     * 删除news
     */
    @PreAuthorize("@ss.hasPermi('system:news:remove')")
    @Log(title = "news", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(newsService.deleteNewsByIds(ids));
    }

    @Log(title = "添加新闻", businessType = BusinessType.INSERT)
    @PostMapping("/addcustom")
    @ResponseBody
    public AjaxResult addCustom(@Validated @RequestBody News news)
    {
        news.setId(UUID.randomUUID().toString());
        news.setDeleted("0");
        if(news.getNumber() == null){
            news.setNumber(new Long(newsService.getMaxNumber() + 1));
        }
        news.setPageView("0");
        news.setCreateTime(new Date());
        news.setUpdateTime(new Date());
        LoginUser loginUser = getLoginUser();
        news.setUpdateBy(loginUser.getUsername());

        return toAjax(newsService.insertNews(news));
    }

    @Log(title = "添加新闻浏览量", businessType = BusinessType.INSERT)
    @PostMapping("/addview")
    @ResponseBody
    public AjaxResult addView(@RequestParam("id") String id)
    {
        News news = newsService.selectNewsById(id);
        String pageView = news.getPageView();
        if(pageView == null){
            pageView = "0";
        }

        news.setPageView(String.valueOf(Integer.parseInt(pageView) + 1));
        return toAjax(newsService.updateNews(news));
    }

    @GetMapping("/findone")
    @ResponseBody
    public News findone(@RequestParam("id") String id)
    {
        return newsService.selectNewsById(id);
    }

    @Log(title = "修改会议", businessType = BusinessType.UPDATE)
    @PostMapping("/modify")
    @ResponseBody
    public AjaxResult modify(@Validated @RequestBody News news)
    {

        News oldNews = newsService.selectNewsById(news.getId());
        news.setCreateTime(oldNews.getCreateTime());

        news.setUpdateTime(new Date());
        LoginUser loginUser = getLoginUser();
        news.setUpdateBy(loginUser.getUsername());

        return toAjax(newsService.updateNews(news));
    }

    @Log(title = "删除会议", businessType = BusinessType.DELETE)
    @PostMapping("/delete")
    @ResponseBody
    public AjaxResult delete(@RequestParam("id") String id)
    {
        News news = newsService.selectNewsById(id);

        news.setUpdateTime(new Date());
        news.setDeleted("1");

        return toAjax(newsService.updateNews(news));
    }

    @GetMapping("/selectall")
    @ResponseBody
    public Map<String, Object> selectall(@RequestParam("newname") String ppnewname,@RequestParam("pageindex") int ppPageIndex, @RequestParam("pagesize") int ppPageSize)
    {
        int mmStartIndex = (ppPageIndex - 1) * ppPageSize;
        if (!"".equals(ppnewname)){
            ppnewname = "%"+ppnewname+"%";
        }

        int mmCount = newsService.countAll(ppnewname);
        Map<String, Object> mmMap = new HashMap<String, Object>();
        PageInfo mmPageInfo = new PageInfo(ppPageIndex, ppPageSize, mmCount);
        List<News> mmInfo = newsService.selectAll( ppnewname, mmStartIndex, ppPageSize);
        mmMap.put("InfoList", mmInfo);
        mmMap.put("PageInfo", mmPageInfo);
        return mmMap;
    }

    @GetMapping("/selectValid")
    @ResponseBody
    public Map<String, Object> selectValid(@RequestParam("newname") String ppnewname,@RequestParam("pageindex") int ppPageIndex, @RequestParam("pagesize") int ppPageSize)
    {
        int mmStartIndex = (ppPageIndex - 1) * ppPageSize;
        if (!"".equals(ppnewname)){
            ppnewname = "%"+ppnewname+"%";
        }

        int mmCount = newsService.countValid(ppnewname);
        Map<String, Object> mmMap = new HashMap<String, Object>();
        PageInfo mmPageInfo = new PageInfo(ppPageIndex, ppPageSize, mmCount);
        List<News> mmInfo = newsService.selectValid( ppnewname, mmStartIndex, ppPageSize);
        mmMap.put("InfoList", mmInfo);
        mmMap.put("PageInfo", mmPageInfo);
        return mmMap;
    }


}
